﻿using mozhi.smarterp.Finance.Basedatas.Repository;
using mozhi.smarterp.Finance.Finances.Repository;
using mozhi.smarterp.Finance.Querys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Domain.Services;

namespace mozhi.smarterp.Finance.Basedatas
{
    public class DomainServiceBase : DomainService
    {
        protected IDataAssociationRepository _dataAssociationRepository;

        public DomainServiceBase(IDataAssociationRepository dataAssociationRepository)
        {
            _dataAssociationRepository = dataAssociationRepository;
        }

        public async Task<List<string>> GetDataAssociationDetail(Guid occupiedId)
        {
            var messages = new List<string>();

            var dataAssociations = await _dataAssociationRepository.FindAsync(new DataAssociationQuery() { OccupiedId = occupiedId });

            if (dataAssociations.Count == 0)
                return messages;

            var groups = dataAssociations.GroupBy(x => x.OccupantType);

            foreach (IGrouping<string, DataAssociation> info in groups)
            {
                var details = info.Select(x => x.OccupantCode + "-" + x.OccupantName).ToList();

                var tips = info.Key + ":" + string.Join(",", details);

                messages.Add(tips);
            }

            return messages;
        }
    }
}
